package validate

import (
	"fmt"
	"github.com/cloudwego/hertz/pkg/app/server/binding"
)

func checkPhone(args ...interface{}) error {

	if len(args) != 1 {
		return fmt.Errorf("手机号必填")
	}
	s, _ := args[0].(string)

	if len(s) != 11 {
		return fmt.Errorf("手机号长度必须是11位")
	}
	for _, v := range s {
		if v < '0' || v > '9' {
			return fmt.Errorf("手机号必须是数字")
		}
	}

	return nil
}

func checkPassword(args ...interface{}) error {

	if len(args) != 1 {
		return fmt.Errorf("密码必填")
	}
	s, _ := args[0].(string)

	if len(s) < 6 {
		return fmt.Errorf("密码长度必须大于6位")
	}

	return nil
}

type ValidateError struct {
	ErrType, FailField, Msg string
}

func (e *ValidateError) Error() string {
	if e.Msg != "" {
		return e.ErrType + ": expr_path=" + e.FailField + ", cause=" + e.Msg
	}
	return e.ErrType + ": expr_path=" + e.FailField + ", cause=invalid"
}

func checkLoginName(args ...interface{}) error {
	return nil
}
func InitValidate() *binding.ValidateConfig {
	validateConfig := &binding.ValidateConfig{}
	validateConfig.MustRegValidateFunc("checkPhone", checkPhone)
	validateConfig.MustRegValidateFunc("checkPassword", checkPassword)
	validateConfig.MustRegValidateFunc("checkLoginName", checkLoginName)

	//validateConfig.SetValidatorErrorFactory(func(failField, msg string) error {
	//	err := ValidateError{
	//		ErrType:   "validateErr",
	//		FailField: "[validateFailField]: " + failField,
	//		Msg:       failField + msg,
	//	}
	//
	//	return &err
	//})
	return validateConfig
}
